/*
    生成.dot
*/
import { statement } from "./cfg";
import { CPG } from "./cpg"; 
import * as fs from 'fs';
import * as path from 'path';
import {ASTree } from './tsAST';
import { NodeA } from "./tsAST";
import {CFG} from"./cfg";
import {DDG} from"./ddg";
import {Program} from './program';
// console.log("digraph G {");


let indir:string = "testfile";
let outdir:string = "dot";
let files = fs.readdirSync(indir);
// getFiletDot("testfile/drag.ts", "dot/drag.dot");
files.forEach(function(item:any,index:any){
    getFiletDot(indir+"/"+item, outdir+"/"+item.slice(0,-2)+"dot");
});

function getFiletDot(testFile:string, outPut:string){
    fs.writeFileSync(outPut, "digraph G {\n", { encoding: 'utf-8' });
    
    let fileContent = fs.readFileSync(testFile, 'utf8');//读取t.ts代码内容
    let ast:ASTree=new ASTree(fileContent);//用代码内容构造AST
    ast.buildTreeGraph(ast.root);
    
    type outSta = Pick<NodeA, "text"|"start"|"index"|"kind">
    for(var sta of ast.stmArray){
        const out:outSta = {
            text: sta.text,
            start: sta.start,
            index: sta.index,
            kind: sta.kind,
        }
     fs.appendFileSync(outPut, "Node"+out.index+" [label=\""+`${out.kind.replace(/"/g, '\\"')}`+"\"][name=\""+`${out.text.replace(/"/g, '\\"')}`+"\"]\n", { encoding: 'utf-8' });

    }
    for(var edge of ast.ed){
    let  a=edge.u;
    let b=edge.v;
        {fs.appendFileSync(outPut, "Node"+a+" -> Node"+b+"  [label=\"ast\",style=\"dotted\"]\n", { encoding: 'utf-8' });
    }}

    let p=new Program(ast.root);
    for(let f of p.functions){
        getDot(ast,f,outPut);
    }
    for(let c of p.classes){
        for(let m of c.methods){
            getDot(ast,m,outPut);
        }
    }

    fs.appendFileSync(outPut, "}\n", { encoding: 'utf-8' });
}
function getDot(ast:ASTree, cfg:CFG, outPut:string){
    let cpg:CPG = new CPG(cfg);

    // console.log(cfg);
    // console.log(cpg);

    for(var edgeCPG of cpg.ed){
        // console.log("Node"+edge.u+" -> Node"+edge.v)
        // console.log(edgeCPG.u)
        // console.log(cpg.stmArray[edgeCPG.u-2])
        let a= 0;
        let b =0;
        if(cpg.stmArray[edgeCPG.u] && cpg.stmArray[edgeCPG.u ].astNode != null){
            let t = cpg.stmArray[edgeCPG.u].astNode;
            if(t!=null)a=t.index;
        }
        if(cpg.stmArray[edgeCPG.v] && cpg.stmArray[edgeCPG.v].astNode != null){
            let t = cpg.stmArray[edgeCPG.v].astNode;
            if(t!=null)b=t.index;
        }
        // console.log(a+" "+b);
        if(a==0||b==0){
            continue;
        }
        if(edgeCPG.type == 0)fs.appendFileSync(outPut, "Node"+a+" -> Node"+b+"  [label=\"cfg\",style=\"bold\"]\n", { encoding: 'utf-8' });
        else if(edgeCPG.type == 1)fs.appendFileSync(outPut, "Node"+a+" -> Node"+b+"  [label=\"cdg\",style=\"dash\",color=\"red\"]\n", { encoding: 'utf-8' });
        else if(edgeCPG.type == 2)fs.appendFileSync(outPut, "Node"+a+" -> Node"+b+"  [label=\"ddg: "+edgeCPG.ident+"\",style=\"dot\",color=\"blue\"]\n", { encoding: 'utf-8' });

    }
    
}